package main.hw1model;

import java.util.List;
import java.util.ArrayList;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import main.util.HibernateContext;

@Entity
@Table(name="AirportCompany")
public class AirportCompany
{
	private long id;
	private String abr;
	private List<Airport> airports = new ArrayList<Airport>();
	
	public AirportCompany(){}
	
	public AirportCompany(String abr)
	{
		this.abr = abr;
	}
	
	@Id
	@GeneratedValue
	@Column(name="id")
	public long getId()
	{
		return id;
	}
	
	public void setId(long id)
	{
		this.id = id;
	}
	
	@Column(name="abr")
	public String getAbr()
	{
		return abr;
	}
	
	public void setAbr(String abr)
	{
		this.abr = abr;
	}
	
	@ManyToMany
	@JoinTable(name="airport_company",
			   joinColumns={@JoinColumn(name="company_id")},
			   inverseJoinColumns={@JoinColumn(name="airport_id")})
	public List<Airport> getAirport()
	{
		return airports;
	}
	
	public void setAirport(List<Airport> airports)
	{
		this.airports = airports;
	}
	
	public static void load()
	{
		Session session = HibernateContext.getSession();
		Transaction tx = session.beginTransaction();
		
		Airport air = Airport.find("Jerry International Airport");
		AirportCompany airComp = new AirportCompany("HA");
		airComp.getAirport().add(air);
		AirportCompany airComp2 = new AirportCompany("WN");
		airComp2.getAirport().add(air);
		air = Airport.find("SJSU Airport");
		airComp.getAirport().add(air);
		AirportCompany airComp3 = new AirportCompany("AA");
		airComp3.getAirport().add(air);
		air = Airport.find("San Francisco Interantional Airport");
		airComp2.getAirport().add(air);
		airComp3.getAirport().add(air);
		tx = session.beginTransaction();
		{
			session.save(airComp);
			session.save(airComp2);
			session.save(airComp3);
		}
		tx.commit();
		session.close();
	}
	
	public static void list()
	{
		Session session = HibernateContext.getSession();
		Criteria crit = session.createCriteria(AirportCompany.class);
		crit.addOrder(Order.asc("id"));
		
		List<AirportCompany> company = crit.list();
		System.out.println("All companies");
		
		for (AirportCompany comp : company)
		{
			comp.print();
			for(Airport air : comp.getAirport())
				System.out.printf("Airports: %s\n", air.getName());
		}
	}
	
	public static AirportCompany find(String abr)
	{
		Session session = HibernateContext.getSession();
		Query query = session.createQuery("from AirportCompany where abr = :abr");
		query.setString("abr", abr);
		AirportCompany company = (AirportCompany)query .uniqueResult();
		session.close();
		return company;
	}
	
	public void print()
	{
		System.out.printf("%d\t%s\n", id, abr);
	}
	
	public void printInSession()
	{
		Session session = HibernateContext.getSession();
		session.update(this);
		print();
		session.close();
	}
}